//
//  ViewController.m
//  LunboDemo
//
//  Created by 伍银荣 on 2020/5/21.
//  Copyright © 2020 com.example. All rights reserved.
//

#import "ViewController.h"

#define imgCount 5

@interface ViewController ()<UIScrollViewDelegate>

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
@property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;

@property (strong, nonatomic) IBOutlet UIScrollView *bottomScrollView;

@property (strong, nonatomic) IBOutlet UIImageView *bottomImgView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    CGFloat imgViewW = self.scrollView.frame.size.width;
    CGFloat imgViewH = 200;
    CGFloat imgViewY = 0;
    //创建imgView,有五张图片显示,所以需要循环创建
    for (int i = 0; i < imgCount; i++) {
        UIImageView *imgView = [[UIImageView alloc]init];
        CGFloat imgViewX = imgViewW * i;
        imgView.frame = CGRectMake(imgViewX, imgViewY, imgViewW, imgViewH);
        imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%02d",(i + 1)]];
        [self.scrollView addSubview:imgView];
        self.scrollView.contentSize = CGSizeMake(imgViewW * imgCount, 0);
    }
    
    //设置scrollView可分页
    self.scrollView.pagingEnabled = YES;
    //取消水平指示器
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    self.pageControl.numberOfPages = 5;
    self.pageControl.currentPage = 0;
    self.scrollView.delegate = self;
    
    //让图片每隔1秒后自动轮播
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    //使用NSRunLoop来运行循环
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    //设置runLoop的优先级(NSRunLoopCommonModes:与别的控件优先级一致)
    [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    
    //设置bottomScollView的contensize
    CGFloat botttomImgMaxY = CGRectGetMaxY(self.bottomScrollView.frame);
    self.bottomScrollView.contentSize = CGSizeMake(imgViewW, botttomImgMaxY);
    
    //self.bottomScrollView.contentOffset = CGPointMake(0, 30);
    
   // self.bottomScrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    
    
}

- (void)timerAction {
    NSInteger currentPage = self.pageControl.currentPage;
    //判断是否是最后一页,如果是最后一页的话,就将当前页甚至为0
    if (currentPage == imgCount - 1) {
        currentPage = 0;
    } else {
        currentPage ++;
    }
    self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * currentPage, 0);
}

/**
 * BUG 1:当拖拽轮播图片几秒不动后,再停止拖拽的话,可以看到紧接着后面一两张图片会快速划过,然后再按照定时器的时间间隔轮播图片.
 * 原因是:当拖拽图片的时候定时器还在跑.
 * 解决办法:当拖拽图片的时候销毁定时器,然后结束拖拽的时候重新开启定时器
 * BUG 2.当在scrolView下面再拖拽一个TableView控件,如果这个时候我在tableView上下滑动的话,会发现上面的轮播图片没有动了,定时器关闭了
 * 原因:因为view这些控件的优先级要大于定时器,网络这些,所以要将定时器的优先级设置和view的优先级一样,这个时候滑动下面的控件,定时器还是可以正常运行了.
 */

#pragma mark - scrollViewWillBeginDragging:视图开始被拖拽
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"视图开始被拖拽");
    [self.timer invalidate];
    //因为定时器已经被销毁了,所以在这里将定时器设置为nil
    self.timer = nil;
}


#pragma mark - scrollViewDidScroll
//在滚动是设置pageControl
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"滚动了");
    //获取当前pageControl的偏移量
    CGFloat imgViewW = self.scrollView.frame.size.width;
    CGFloat offset = self.scrollView.contentOffset.x + (imgViewW * 0.5);
    int currentPage = offset / imgViewW;
    self.pageControl.currentPage = currentPage;
}


#pragma mark - scrollViewWillEndDragging:视图结束拖拽
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {
    NSLog(@"------结束拖拽-----");
    //因为定时器已经被销毁了,这个时候需要重新创建定时器,然后开启定时器
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    //使用NSRunLoop来运行循环
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    //设置runLoop的优先级(NSRunLoopCommonModes:与别的控件优先级一致)
    [runLoop addTimer:self.timer forMode:NSRunLoopCommonModes];
}


@end
